`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    14:53:07 11/10/2011 
// Design Name: 
// Module Name:    collisionDetection 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module collisionDetection(clk, CX, CY, objX, objY, ball_X, ball_Y, P1_paddle, P2_paddle, tile, draw_enable);
    
	 input clk;
	 input [9:0] CX;
	 input [8:0] CY;
    
    input [9:0] ball_X;
    input [8:0] ball_Y;
    input [8:0] P1_paddle;
    input [8:0] P2_paddle;
	 
    output reg [3:0] tile = 0;    
	 output reg [9:0] objX = 0;
    output reg [8:0] objY = 0;
	 output reg draw_enable = 0;

parameter paddle_height = 96; //pixels   
parameter paddle_width = 32; //32x32 pixel

//
// tile 0, ball
// tile 1, P1 paddle
// tile 2, P2 paddle (invertion/fliped along y axis (16pixel paddle))
// ...... Different tiles are chosen in VGA Controller this just sets which type of tile to create.
// ......

parameter P1paddle = 1;
parameter P2paddle  = 2;
parameter ball = 0;

always @(posedge clk) begin

	//layers drawing priority (high to low)
	///
	/// 1) p1 paddle
	/// 1.5) p2 paddle
	/// 2) ball
	/// 3) bg
	
	//p1 paddle, if left side scree, below y position and above the height...
	if(CX < paddle_width && CY >= P1_paddle && CY < (paddle_height+P1_paddle)) begin
		
		//check p1 current height pattern for multiple pixels, maximum of 96pixels
		if(CY > (P1_paddle+64)) begin
			draw_enable <= 1;
			objX <= 0;
			objY <= (P1_paddle+64);	
			tile <= P1paddle;
		
		end
		
		else if(CY > (P1_paddle+32)) begin
			draw_enable <= 1;
			objX <= 0;
			objY <= (P1_paddle+32);	
			tile <= P1paddle;
		
		end
		
		else begin //pixel is default
			draw_enable <= 1;
			objX <= 0;
			objY <= P1_paddle;	
			tile <= P1paddle;
			
		end
	
	end
	
	//p2 paddle, if left side scree, below y position and above the height...
	else if(CX >= (640-paddle_width) && CY >= P2_paddle && CY < (paddle_height+P2_paddle)) begin
		
		//check p1 current height pattern for multiple pixels, maximum of 96pixels
		if(CY > (P2_paddle+64)) begin
			draw_enable <= 1;
			objX <= 608;
			objY <= (P2_paddle+64);	
			tile <= P2paddle;
		
		end
		
		else if(CY > (P2_paddle+32)) begin
			draw_enable <= 1;
			objX <= 608;
			objY <= (P2_paddle+32);	
			tile <= P2paddle;
		
		end
		
		else begin //pixel is default
			draw_enable <= 1;
			objX <= 608;
			objY <= P2_paddle;	
			tile <= P2paddle;
			
		end
	
	end
	
	//ball
	else if(CX >= (ball_X-3) && CX < (ball_X + 31) && CY >= ball_Y && CY < (ball_Y+32)) begin
		
		draw_enable <= 1;
		objX <= ball_X;
		objY <= ball_Y;	
		tile <= ball;
	
	end
	
	else begin//do nothing
		draw_enable <= 0;
	end

end

endmodule
